import { createStore } from 'redux'

let reducer = (state, action) => {
    // 先copy一份
    let NewState = JSON.parse(JSON.stringify(state))
    switch(action.type) {
        case 'ADD_DATA_ITEM':
            NewState.data.push(action.list)
            return NewState
        case 'DEL_DATA_ITEM':
            NewState.data.splice(action.index, 1)
            return NewState
        // 编辑
        case 'EDIT_ITEM':
            NewState.data = NewState.data.map(item => item.key === action.item.key? action.item: item)
            return NewState
        // 搜索
        case 'SEARCH_VAL':
            NewState.data = NewState.data.filter(item => item.acronym.includes(action.val))
            return NewState
        default:
            return state
    }
}

let initState = {
    data: [
        {
            key: '1',
            name: '前端',
            acronym: 'frontEnd',
            article: 2
        },
        {
            key: '2',
            name: '后端',
            acronym: 'backEnd',
            article: 3
        },
        {
            key: '3',
            name: '开发工具',
            acronym: 'tool',
            article: 4
        },
        {
            key: '4',
            name: '数据库',
            acronym: 'database',
            article: 26
        }
    ]
}

let store = createStore(reducer, initState)

export default store